﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_DeliveryWay_Manage : System.Web.UI.Page
{
    public int DeliID
    {
        get { return Convert.ToInt32(ViewState["_ID"]); }
        set { ViewState["_ID"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DeliID = DongDong.Common.Misc.GetQueryStringAsInt("ID");
            if (DeliID != 0)
                GetWayInfo();
        }
    }
    protected void btn_search_Click_Click(object sender, EventArgs e)
    {
        DeliveryWay objWay = new DeliveryWay();
        if (DeliID != 0)
        {
            objWay = DeliveryWay.Get(DeliID);
        }
        objWay.Way = txtPeiSong.Text;
        objWay.Regin = txtFanWei.Text;
        objWay.Price = DongDong.Common.Misc.ConvertToDecimal(txtPrice.Text);
        objWay.DeliveryTime = txtTime.Text;
        objWay.Rank = System.DateTime.Now;
        if (DeliID != 0)
        {
            objWay.Update();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "success2", "<script>alert('修改成功')</script>");
        }
        else
        {
            objWay.Add();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "success1", "<script>alert('添加成功')</script>");
        }
        PopulateWayList();
    }

    public void PopulateWayList()
    {
        Lv_WayList.DataSource = DeliveryWay.GetAll();
        Lv_WayList.DataBind();
    }


    public void GetWayInfo()
    {
        DeliveryWay objWay = DeliveryWay.Get(DeliID);
        if (objWay != null)
        {
            txtPeiSong.Text = objWay.Way ?? "";
            txtFanWei.Text = objWay.Regin ?? "";
            txtPrice.Text = objWay.Price.ToString() ?? "";
            txtTime.Text = objWay.DeliveryTime ?? "";
        }
    }
    protected void Lv_WayList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        DeliveryWay.Delete(DongDong.Common.Misc.ConvertToInt(Lv_WayList.DataKeys[e.ItemIndex].Value));
        PopulateWayList();
    }
    protected void Lv_WayList_PagePropertiesChanged(object sender, EventArgs e)
    {
        PopulateWayList();
    }
    protected void Pager_PreRender(object sender, EventArgs e)
    {
        if (Lv_WayList.Items.Count == 0)
            PopulateWayList();
    }
}